/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author mamat-dev
 */
public class List extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here*/
            out.println("<html>");
            out.println("<head>");
            out.println("<title>List Data Table</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<table border=1>");
            out.println("   <tr>");
            out.println("       <td align='center'>NO</td>" +
                    "           <td align='center'>MESSAGE</td>" +
                    "       </tr>");
            try {
                /* TODO output your page here */
                Connection con = null;
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                } catch (ClassNotFoundException cnf) {
                    System.out.println("ERror:" + cnf);
                }
                try {
                    String strCon = "jdbc:mysql://localhost:3306/test";
                    con = DriverManager.getConnection(strCon, "root", "112233");
                } catch (SQLException se) {
                    System.out.println("Koneksi gagal" + se);
                }

                Statement st = con.createStatement();
                String message = request.getParameter("message");
                ResultSet rs = st.executeQuery("select * from testtb");
                while(rs.next()){
                    out.println("<tr>" +
                            "       <td>"+rs.getString(1)+"</td>" +
                            "       <td>"+rs.getString(2)+"</td>" +
                            "    </tr>");
                }
            } catch (Exception e) {
                out.print("insert gagal");
            } finally {
                out.close();
            }
            out.println("</table>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
